package com.jay.compass;

import com.jay.compass.adapter.RecentChatAdapter;
import com.jay.compass.app.StarApplication;
import com.jay.compass.base.RecentItem;
import com.jay.compass.dialog.SendBeidouDialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

public class FragmentChat extends Fragment {

	private ListView recentChatListView;
	private StarApplication mApplication;
	private SendBeidouDialog mSendBeidouDialog;

	private FragmentCallbackListener mListener;
	private RecentChatAdapter mAdapter;

	public static final String USER_ID = "USER_ID";
	public static final String USER_NAME = "USER_NAME";
	private int mPos = -1;

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
		mListener = (FragmentCallbackListener) activity;
	}

	public void onSecondPast(int time) {
		if (mSendBeidouDialog.isShowing()) {
			mSendBeidouDialog.onSecondPast(time);
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View rootView = inflater.inflate(R.layout.fragment_chat, container,
				false);

		recentChatListView = (ListView) rootView
				.findViewById(R.id.recentChatList);
		mAdapter = new RecentChatAdapter(getActivity(), null);
		recentChatListView.setAdapter(mAdapter);
		recentChatListView.setCacheColorHint(0);

		recentChatListView.setOnItemClickListener(new OnItemClick());
		mSendBeidouDialog = new SendBeidouDialog(getActivity());
		mSendBeidouDialog.setOnCancelListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mSendBeidouDialog.dismiss();
			}
		});

		mSendBeidouDialog.setOnSendListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				mListener.sendToBeidou(mSendBeidouDialog.getId(),
						mSendBeidouDialog.getContent());
				mSendBeidouDialog.dismiss();
				mSendBeidouDialog.clearContent();
			}
		});

		recentChatListView
				.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {
						// TODO Auto-generated method stub
						System.out
								.println("pos = " + position + ", id = " + id);
						if (position > 0) {
							mPos = position;
							RecentItem item = StarApplication.getInstance()
									.getRecentDB().getRecentList()
									.get(position - 1);

							AlertDialog isExit = new AlertDialog.Builder(
									getActivity()).create();
							isExit.setTitle("提示");
							isExit.setMessage("确定删除" + item.getName() + "的消息?");
							isExit.setButton("确定", exitListener);
							isExit.setButton2("取消", exitListener);
							isExit.show();
						}
						return true;
					}
				});

		return rootView;
	}

	DialogInterface.OnClickListener exitListener = new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
			// TODO Auto-generated method stub
			if (which == AlertDialog.BUTTON_POSITIVE) {
				RecentItem item = StarApplication.getInstance().getRecentDB()
						.getRecentList().get(mPos - 1);
				StarApplication.getInstance().getRecentDB()
						.delRecent(item.getUserId());
				StarApplication.getInstance().getMessageDB()
						.delSession(item.getUserId());
				mAdapter.notifyDataSetChanged();
			} else if (which == AlertDialog.BUTTON_NEGATIVE) {
				;
			}
		}
	};

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		updateRecentList();
	}

	public void updateRecentList() {
		mAdapter.notifyDataSetChanged();
		// mAdapter.notifyDataSetInvalidated();
	}

	private class OnItemClick implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View view, int position,
				long id) {
			// TODO Auto-generated method stub
			if (position == 0) {
				mSendBeidouDialog.show();
			} else {
				RecentItem item = StarApplication.getInstance().getRecentDB()
						.getRecentList().get(position - 1);
				// clear this recent item new num
				StarApplication.getInstance().getRecentDB()
						.setRecentNewNum(item, 0);

				Intent intent = new Intent(getActivity(), ChatActivity.class);
				intent.putExtra(USER_ID, item.getUserId());
				intent.putExtra(USER_NAME, item.getName());
				mListener.leaveMainCallback();
				startActivity(intent);
			}
		}
	}
}
